package model;

import java.sql.*;
import java.util.Vector;

public class CommunityCategoryDAO {
	
	//커뮤니티 카테고리 등록
	public boolean insert(CommunityCategory cc) {
		Connection conn = ConnectionManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("insert into communitycategory(ccname) values(?)");			
			ps.setString(1, cc.getCcname());		

			ps.executeUpdate();

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}

	//커뮤니티 카테고리 삭제
	public boolean delete(CommunityCategory cc) {
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement st = conn.createStatement();
			st.executeUpdate("delete from communitycategory where ccno=" + cc.getCcno());

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}

	//커뮤니티 카테고리 
	public boolean updateCommunityCategory(CommunityCategory cc) {
		Connection conn = ConnectionManager.getConnection();				
		try {			
			PreparedStatement ps = conn.prepareStatement("update communitycategory set ccname=? where ccno="+cc.getCcno());
			ps.setString(1, cc.getCcname());
			ps.executeUpdate();
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}	
	
	//전체 커뮤니티 카테고리 수 
	public int getTotalRows() {
		int totalRows = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select count(*) from communitycategory");
			rs.next();
			totalRows = rs.getInt(1);
			rs.close();
			st.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return totalRows;
	}

	//커뮤니티 카테고리 상세 보기 
	public CommunityCategory getCommunityCategory(int ccno) {
		Connection conn = ConnectionManager.getConnection();
		CommunityCategory category = null;
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st
					.executeQuery("select * from communitycategory where ccno=" + ccno);
			if (rs.next()) {
				category = new CommunityCategory();
				category.setCcno(rs.getInt(1));
				category.setCcname(rs.getString(2));
			}
			rs.close();
			st.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return category;
	}
	
	//커뮤니티 카테고리 상세 보기 (조성일제작)
	public CommunityCategory getCommunityCategory(String ccname) {
		
		Connection conn = ConnectionManager.getConnection();
		CommunityCategory category = null;
		
		try {
			
			String sql = "select ccno,ccname from communitycategory where ccname='" + ccname+"'";
			System.out.println(sql);
			
			Statement st = conn.createStatement();
			ResultSet rs = st
					.executeQuery(sql);
			if (rs.next()) {
				category = new CommunityCategory();
				category.setCcno(rs.getInt(1));
				category.setCcname(rs.getString(2));
			}
			rs.close();
			st.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return category;
	}
	
	
	

	//커뮤니티 카테고리 목록 
	public Vector getList() {
		Vector list = new Vector();
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select * from communitycategory where ccno != 1 order by ccno");
			while (rs.next()) {
				CommunityCategory cc = new CommunityCategory();
				cc.setCcno(rs.getInt(1));
				cc.setCcname(rs.getString(2));
				
				list.add(cc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return list;
	}
	
	
	/*public static void main(String arg[])
	{
		CommunityCategoryDAO ccDAO = new CommunityCategoryDAO();
		CommunityCategory cc = new CommunityCategory();
		cc = ccDAO.getCommunityCategory("스포츠");
		System.out.println("ccno"+cc.getCcno());
		System.out.println("ccname"+cc.getCcname());
	}*/

}
